#include "SDLWrapper.h"
#include <iostream>

SDLWrapper::SDLWrapper() {
	this->screenTitle = GAME_NAME;
	this->widthResolution = LOAD_WIDTH_RESOLUTION;
	this->heightResolution = LOAD_HEIGHT_RESOLUTION;
	this->bitsPerPixel = BITS_PER_PIXEL;
	this->playerName = STRING_EMPTY;
}

SDLWrapper::SDLWrapper(std::string screenTitle, int widthResolution,
		int heightResolution, int bitsPerPixel) {

	//Attach the game name to the screenTitle
	this->screenTitle = GAME_TITLE + screenTitle;
	this->widthResolution = widthResolution;
	this->heightResolution = heightResolution;
	this->bitsPerPixel = bitsPerPixel;
	this->playerName = STRING_EMPTY;
}

SDLWrapper::~SDLWrapper() {
	delete (this->screen);
	SDL_Quit();
}

int SDLWrapper::pullEvent(Event** event) {
	SDL_Event sdlEvent;
	int ret = SDL_PollEvent(&sdlEvent);
	if (ret) {
		//Delete previous reference if exists
		if(*event != NULL)
			delete *event;

		*event = new Event(sdlEvent);
	}
	return ret;
}

bool SDLWrapper::start() {
	if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
		return false;
	}

	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);

	screen = new Surface();
	screen->SetVideoMode(this->widthResolution, this->heightResolution,
			this->bitsPerPixel, SDL_SWSURFACE);

	if (screen == NULL) {
		return false;
	}
	if (TTF_Init() == -1) {
		return false;
	}
	char* c_screenTitle = new char[this->screenTitle.size() + 1];
	strcpy(c_screenTitle, this->screenTitle.c_str());
	SDL_WM_SetCaption(c_screenTitle, NULL);
	delete[] c_screenTitle;


	return true;
}


void SDLWrapper::stop() {
	TTF_Quit();
}

void SDLWrapper::setPlayerName(std::string playerName) {
	this->playerName = playerName;
	std::stringstream ss;
	ss << "PlayerName: " << playerName;
	Logger::log("SDLEngine", "setPlayerName", &ss);
}

void SDLWrapper::clearScreen() {
	SDL_FillRect(screen->getSurface(), &screen->getSurface()->clip_rect,
				SDL_MapRGB(screen->getSurface()->format, 0, 0, 0));
}

void SDLWrapper::drawSurface(Surface* surface, SDL_Rect rect) {
	SDL_Surface* sfc = surface->getSurface();
	if (sfc != NULL) {
		SDL_BlitSurface(sfc, NULL, this->screen->getSurface(), &rect);
	} else {
		Logger::log("SDLEngine", "drawSurface",	"La Surface no se obtuvo correctamente");
	}
}

void SDLWrapper::refreshScreen() {
	if (SDL_Flip(this->screen->getSurface()) != 0) {
		Logger::log("SDLEngine", "refreshScreen","no se pudo refrescar la pantalla");
	}
}

void SDLWrapper::updateVideoMode(int w, int h) {
	this->screen->SetVideoMode(w, h, BITS_PER_PIXEL,
			SDL_SWSURFACE | SDL_RESIZABLE);
	this->widthResolution = w;
	this->heightResolution = h;
	char* c_screenTitle = new char[this->screenTitle.size() + 1];
	strcpy(c_screenTitle, this->screenTitle.c_str());
	SDL_WM_SetCaption(c_screenTitle, NULL);
	delete[] c_screenTitle;
	SDL_FillRect(screen->getSurface(), &screen->getSurface()->clip_rect,
			SDL_MapRGB(screen->getSurface()->format, 233, 233, 233));
}

Surface* SDLWrapper::getMainSurface() {
	return this->screen;
}

void SDLWrapper::changeScreenTitle(std::string screenTitle) {
	this->screenTitle = screenTitle;
}
